//
//  HTClassWebVC.swift
//  QuitSmokingApp
//  
//  Created by wealon on 2024.
//  QuitSmokingApp.
//  
    

import UIKit
import WebKit

class HTClassWebVC: HTClassViewController {
    lazy var var_webView: WKWebView = {
        let var_config = WKWebViewConfiguration()
        let var_userContent = WKUserContentController()
        var_config.userContentController = var_userContent
        let var_web = WKWebView(frame: .zero, configuration: var_config)
        var_web.navigationDelegate = self
        return var_web
    }()
    lazy var var_back: UIButton = {
        let var_button = UIButton()
        var_button.frame = CGRect(x: 0, y: 0, width: 24.auto(), height: 24.auto())
        var_button.setImage(UIImage(named: "icon_black_back"), for: .normal)
        var_button.onTap { [weak self] in
            self?.navigationController?.popViewController(animated: true)
        }
        return var_button
    }()
    var var_url: String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: self.var_back)
        self.var_bg_imageView.isHidden = true
        self.view.backgroundColor = .white
        ht_initNavBar()
        ht_startLoading()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if let var_nav = self.navigationController as? HTClassNavigationController {
            var_nav.var_back.setImage(UIImage(named: "icon_white_back"), for: .normal)
        }
    }
    override func ht_setUI() {
        super.ht_setUI()
        self.view.addSubview(var_webView)
        var_webView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.leading.equalTo(view.safeAreaLayoutGuide.snp.leading)
            make.trailing.equalTo(view.safeAreaLayoutGuide.snp.trailing)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
    func ht_startLoading() {
        if let var_url = URL(string: self.var_url) {
            let var_request = URLRequest(url: var_url)
            var_webView.load(var_request)
        }
    }
    func ht_initNavBar() {
        let var_appearance = UINavigationBarAppearance()
        var_appearance.configureWithOpaqueBackground()
        var_appearance.backgroundColor = .clear
        
        let var_navigationBarAppearance = UINavigationBarAppearance()
        var_navigationBarAppearance.configureWithOpaqueBackground()
        var_navigationBarAppearance.backgroundColor = .clear
        var_navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 20, weight: .bold) ]
        
        self.navigationController?.navigationBar.standardAppearance = var_appearance
        self.navigationController?.navigationBar.compactAppearance = var_navigationBarAppearance
        self.navigationController?.navigationBar.scrollEdgeAppearance = var_navigationBarAppearance
    }
    
}

extension HTClassWebVC: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        if let title = webView.title {
            self.title = title
        }
    }
}

